﻿using StartEAS_API.Utils;
using StartsEAS_API.DAL.DALTools;
using StartsEAS_API.Model;
using StartsEAS_API.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StartsEAS_API.DAL.AlertsD
{
    public class AlertsDAL
    {
        private readonly StarsEASContext _context;
        public AlertsDAL(StarsEASContext context) { 
            _context = context;
        }

        public ResultUtils GetAllAlert() {
            StarsEASContext _context = new StarsEASContext();
            var alertsList = from alter in _context.Alerts
                             join notice in _context.Notices on alter.NoticeId equals notice.NoticeId
                             join worker in _context.Workers on alter.WorkerId equals worker.WorkerId
                             select new
                             {
                                 AlertsTitle = alter.AlertsTitle,
                                 AlertsContent = alter.AlertsContent,
                                 NoticeName = notice.NoticeName,
                                 WorkerName = worker.WorkerName,
                                 StudentShow = alter.StudentShow,
                                 AlterToApplyForTime = alter.AlterToApplyForTime
                             };
            if (alertsList == null)
            {
                AppLog.WriteError($"查询消息通知---未查询到任何消息通知", true);
                return new ResultUtils() { code = 500, message = "您所查询的数据为空" };
            }
            AppLog.WriteInfo($"查询消息通知---查询到{alertsList.Count()}条消息通知", true);
            return new ResultUtils() { code = 200, message = alertsList };
        }

        public ResultUtils InsertAlerts(Alert data) {
            StarsEASContext _context = new StarsEASContext();
            data.AlertsId = 0;
            Console.WriteLine(data.AlertsTitle);
            Console.WriteLine(data.AlertsContent);
            _context.Alerts.Add(data);
            int result = _context.SaveChanges();
            if (result == 0)
            {
                AppLog.WriteError($"添加消息通知---{data.WorkerId}添加消息{data.AlertsTitle}：{data.AlertsContent}消息失败", true);
                return new ResultUtils() { code = 500, message = result };
            }
            AppLog.WriteInfo($"添加消息通知---{data.WorkerId}添加消息{data.AlertsTitle}：{data.AlertsContent}消息成功", true);
            return new ResultUtils() { code=200,message=data};
        }

        public ResultUtils UpdateAlerts(Alert data) {
            StarsEASContext _context = new StarsEASContext();
            Alert alert = _context.Alerts.Where<Alert>(a => a.AlertsId == data.AlertsId).FirstOrDefault();
            if (alert == null)
            {
                AppLog.WriteError($"修改消息通知---未查询到{data.AlertsId}这条消息，修改失败", true);
                return new ResultUtils() { code = 500, message = "null" };
            }
            alert.AlertsTitle = data.AlertsTitle??alert.AlertsTitle;
            alert.AlertsContent = data.AlertsContent??alert.AlertsContent;
            alert.AlterToApplyForTime = data.AlterToApplyForTime??alert.AlterToApplyForTime;
            if (data.NoticeId != 0) alert.NoticeId = data.NoticeId;
            if (!data.WorkerId.Equals("")) alert.WorkerId = data.WorkerId;
            if (data.StudentShow != 0) alert.StudentShow = data.StudentShow;
            int result = _context.SaveChanges();
            if (result == 0) {
                AppLog.WriteError($"修改消息通知---{alert.AlertsId}消息修改失败或未有任何更改", true);
                return new ResultUtils { code = 500, message = "消息修改失败或未有任何更改" };
            }
            AppLog.WriteInfo($"修改消息通知---消息{alert.AlertsId}消息修改成功", true);
            return new ResultUtils() { code = 200, message = alert };
        }

        public ResultUtils GetAllAlertByStudent()
        {
            StarsEASContext _context = new StarsEASContext();
            List<Alert> alertsList = _context.Alerts.Where(a=>a.StudentShow==int.Parse(AlertsStudentIsShow.SHOW.ToString())).ToList();
            if (alertsList == null)
            {
                AppLog.WriteError($"查询消息通知---未查询到任何学生可见的消息通知", true);
                return new ResultUtils() { code = 500, message = alertsList };
            }
            AppLog.WriteInfo($"查询消息通知---查询到{alertsList.Count()}条学生可见的消息通知", true);
            return new ResultUtils() { code = 200, message = alertsList };
        }

        public ResultUtils GetAllAlertByToken(string token) {
            StarsEASContext _context = new StarsEASContext();
            User user = _context.Users.Where(u => u.Token.Equals(token)).FirstOrDefault();
            if (user == null)
            {
                AppLog.WriteError($"查询消息通知---账户{token}尝试登录但登录失败", true);
                return new ResultUtils() { code = 500, message = "Token无效" };
            }
            if (token.IsStudent(_context))
            {
                var alertsList = from alter in _context.Alerts
                                 join notice in _context.Notices on alter.NoticeId equals notice.NoticeId
                                 join worker in _context.Workers on alter.WorkerId equals worker.WorkerId
                                 where alter.StudentShow == (int)AlertsStudentIsShow.SHOW
                                 select new
                                 {
                                     AlertsTitle = alter.AlertsTitle,
                                     AlertsContent = alter.AlertsContent,
                                     NoticeName = notice.NoticeName,
                                     WorkerName = worker.WorkerName,
                                     StudentShow = alter.StudentShow,
                                     AlterToApplyForTime = alter.AlterToApplyForTime
                                 };
                AppLog.WriteInfo($"查询消息通知---查询到{alertsList.Count()}条学生可见的消息通知", true);
                return new ResultUtils() { code = 200, message = alertsList };
            }
            else {
                var alertsList = from alter in _context.Alerts
                             join notice in _context.Notices on alter.NoticeId equals notice.NoticeId
                             join worker in _context.Workers on alter.WorkerId equals worker.WorkerId
                             select new
                             {
                                 AlertsTitle = alter.AlertsTitle,
                                 AlertsContent = alter.AlertsContent,
                                 NoticeName = notice.NoticeName,
                                 WorkerName = worker.WorkerName,
                                 StudentShow = alter.StudentShow,
                                 AlterToApplyForTime = alter.AlterToApplyForTime
                             };
                AppLog.WriteInfo($"查询消息通知---查询到{alertsList.Count()}条消息通知", true);
                return new ResultUtils() { code = 200, message = alertsList };
            }
            
        }

    }
}
